package eu.reply.android.flashm;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;

import android.util.Log;
import eu.reply.android.flashm.common.HttpConnection;
import eu.reply.android.flashm.common.HttpError;
import eu.reply.android.flashm.common.HttpRequest;
import eu.reply.android.flashm.common.HttpResponse;
import eu.reply.android.flashm.common.HttpRequest.MessageType;
import eu.reply.android.flashm.entities.Event;
import eu.reply.android.flashm.entities.Profile;
import eu.reply.android.flashm.exceptions.BEServiceNetException;

public class BEAPI {


	public static final String TAG="BEAPI";
	
	public static final String HOST="http://flashmorg.cloud.reply.eu/";//"http://m.google.com";
	public static final String URL_GET_EVENT_LIST=HOST+"flashmob/rest/events";
	public static final String URL_GET_EVENT=HOST+"flashmob/rest/event/";
	public static final String URL_ATTEND_EVENT=HOST+"";
	public static final String URL_GET_PROFILE=HOST+"";
	public static final String URL_REGISTER_USER=HOST+"flashmob/rest/profile/register";
	
	/**
	 * Get the list of available flash mobs
	 * 
	 * @return The list of events or null if there was an error (e.g. HTTP 400
	 *         or HTTP 500)
	 * 
	 * @throws BEServiceNetException
	 *             if there was a connection problem
	 */
	public static ArrayList<Event> getEventList() throws BEServiceNetException {
		ArrayList<Event> events = new ArrayList<Event>();

		Event event1 = new Event(0, "Ronf ronf ronf...");
		event1.setDate(new Date());
		event1.setDescription("Ok, guys! We are going to gather in the new store at the city center and start to sleep in the afternoon. Anybody welcome to join. Please check the date. Do not be late!");
		event1.setImageResourceId(R.drawable.sleep);
		event1.setLatLongt("52.500862, 13.450665");
		events.add(event1);

		Event event2 = new Event(1, "Flash Mob 1");
		event2.setDate(new Date());
		event2.setDescription("Description 1...");
		event2.setImageResourceId(R.drawable.flashm1);
		event2.setLatLongt("52.500862, 13.450665");
		events.add(event2);

		Event event3 = new Event(2, "Flash Mob 2");
		event3.setDate(new Date());
		event3.setDescription("Description 2...");
		event3.setImageResourceId(R.drawable.flashm2);
		event3.setLatLongt("52.500862, 13.450665");
		events.add(event3);

		Event event4 = new Event(3, "Flash Mob 3");
		event4.setDate(new Date());
		event4.setDescription("Description 3...");
		event4.setImageResourceId(R.drawable.flashm4);
		event4.setLatLongt("52.500862, 13.450665");
		events.add(event4);

		URL url = null;
		try {
			url = new URL(URL_GET_EVENT_LIST);
		} catch (MalformedURLException ignore) {
			// TODO check for valid URL
		}

		HttpRequest req = new HttpRequest(MessageType.GET, url);
		HttpConnection conn = new HttpConnection(req);
		HttpResponse res = null;
		res = conn.connect();

		if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
			// HTTP success
			String jsonString = res.toString();
			Log.d(TAG, res.toString());
			
			//TODO parse the json inside jsonString
			/*
			ArrayList<Event> dummy= new ArrayList<Event>(10);
			for(int i=0;i<10;i++){
				Event e = new Event(i,"Flash-M-event:"+i);
				e.setDate(new Date());
				e.setDescription("Description " + i);
				e.setLatLongt("54.123922, 13.841221"); // Usedom FlashMob
				if (i==1) e.setLatLongt("52.500862, 13.450665"); // nhow FlashMob
				dummy.add(e);
			}
			
			events = dummy;
			*/
		}else{
			//HTTP error
			HttpError error = res.getError();
			Log.i(TAG, "HttpError: " + error.getContent());
		}

		return events;
	}

	/**
	 * Fetch the Details of the specified event
	 * 
	 * @param eventName
	 *            The name of the requested event
	 * @return The Event in question of null if there was an error (e.g. HTTP
	 *         400 OR HTTP 500)
	 * @throws BEServiceNetException
	 *             if there was a connection problem
	 */
	public static Event getEvent(String eventName) throws BEServiceNetException {
		Event event = null;

		URL url = null;
		try {
			url = new URL(URL_GET_EVENT);
		} catch (MalformedURLException ignore) {
			// TODO check for valid URL
		}

		HttpRequest req = new HttpRequest(MessageType.GET, url);
		HttpConnection conn = new HttpConnection(req);
		HttpResponse res = null;
		res = conn.connect();

		if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
			// HTTP success
			String jsonString = res.toString();
			Log.d(TAG, res.toString());

			// TODO parse the json inside jsonString
			Event dummy = new Event(54321, "Flash nhow");
			event = dummy;
		} else {
			// HTTP error
			HttpError error = res.getError();
			Log.i(TAG, "HttpError: " + error.getContent());
		}

		return event;
	}

	/**
	 * Mark attending status: attending or not attending
	 * 
	 * @param profileName
	 *            The profile name of the user
	 * @param eventName
	 *            The flash-mob name
	 * @param accepted
	 *            If true, it means that the user is attending. If false the
	 *            user is not attending
	 * @return Zero if the call was successful or different than zero if error
	 *         (e.g. HTTP 400 OR HTTP 500)
	 * 
	 * @throws BEServiceNetException
	 *             If there is a connection problem
	 */
	public static int attendEvent(String profileName, String eventName,
			boolean accepted) throws BEServiceNetException {
		int retVal = 1;

		URL url = null;
		try {
			url = new URL(URL_ATTEND_EVENT);
		} catch (MalformedURLException ignore) {
			// TODO check for valid URL
		}

		HttpRequest req = new HttpRequest(MessageType.GET, url);
		HttpConnection conn = new HttpConnection(req);
		HttpResponse res = null;
		res = conn.connect();

		if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
			// HTTP success
			// String jsonString = res.toString();
			// Log.d(TAG, res.toString());

			// TODO parse the json inside jsonString
			retVal = 0;
		} else {
			// HTTP error
			HttpError error = res.getError();
			Log.i(TAG, "HttpError: " + error.getContent());
		}

		return retVal;
	}

	/**
	 * Fetch the details of the user's profile
	 * 
	 * @param profileName
	 *            The name of the user's profile
	 * @return The profile details or null if there was an error (e.g. HTTP 400
	 *         OR HTTP 500)
	 * @throws BEServiceNetException
	 *             If there is a connection problem
	 */
	public static Profile getProfile(String profileName)
			throws BEServiceNetException {
		Profile profile = null;

		URL url = null;
		try {
			url = new URL(URL_GET_PROFILE);
		} catch (MalformedURLException ignore) {
			// TODO check for valid URL
		}

		HttpRequest req = new HttpRequest(MessageType.GET, url);
		HttpConnection conn = new HttpConnection(req);
		HttpResponse res = null;
		res = conn.connect();

		if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
			// HTTP success
			String jsonString = res.toString();
			Log.d(TAG, res.toString());

			// TODO parse the json inside jsonString
			Profile dummy = new Profile(1234, "Profile Temoc");
			profile = dummy;
		} else {
			// HTTP error
			HttpError error = res.getError();
			Log.i(TAG, "HttpError: " + error.getContent());
		}

		return profile;
	}

	/**
	 * Register a user (sign-in) the user. If the user exists on the backend,
	 * the googleRegID is updated
	 * 
	 * @param profileName
	 *            the name of the user profile
	 * @param googleRegID
	 *            the google id assigned to this device when registering for
	 *            Google Cloud messaging
	 * @return Zero if success. Different than Zero if there was an error (e.g.
	 *         HTTP 400 or HTTP 500)
	 * @throws BEServiceNetException
	 *             If there was a connection problem
	 */

	public static int registerUser(String profileName,String googleRegID) throws BEServiceNetException{
		return 0;
		//		int retVal=1;
//		
//		URL url=null;
//		try {
//			url = new URL(URL_REGISTER_USER);
//			Log.i(TAG,"Url register: "+URL_REGISTER_USER);
//		} catch (MalformedURLException ignore) {
//			//TODO check for valid URL
//		}
//		
//		String json = "{ \"name\":\""+profileName+"\",\"regId\":\""+googleRegID+"\"}";
//		Log.i(TAG, json);
//		
//		HttpRequest req = new HttpRequest(MessageType.POST,url);
//		req.setContent(json.getBytes());
//		HttpConnection conn = new HttpConnection(req);
//		HttpResponse res=null;
//		res = conn.connect();
//				
//		if(res.getStatusCode()>=200 && res.getStatusCode()<300 ){
//			//HTTP success
//			retVal=0;
//		}else{
//			//HTTP error
//			HttpError error = res.getError();
//			Log.i(TAG,"HttpError: "+error.getContent());
//		}
//		
//		return retVal;

	}
}
